//
//  MyMailViewController.m
//  AsiaScene
//
//  Created by Rainbow on 4/2/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "MyMailViewController.h"

@implementation MyMailViewController

- (id)init
{
    self = [super init];
    if (self) {
        self.mailComposeDelegate = self;
        self.navigationBar.barStyle = UIBarStyleBlackOpaque;
    }
    return  self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationBar setTintColor:[UIColor colorWithRed:215/255.0 green:30/255.0 blue:60/255.0 alpha:1]];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}

- (void)cancelEMail {
	[self dismissModalViewControllerAnimated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if(alertView.tag == MFMailComposeResultSaved || alertView.tag == MFMailComposeResultSent) {
		[self cancelEMail];
	}
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
	// Notifies users about errors associated with the interface
	switch (result)
	{
		case MFMailComposeResultCancelled:
			[self cancelEMail];
			break;
		case MFMailComposeResultSaved:
		{
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Save Draft"
															message:@"Save Draft Success"
														   delegate:self
												  cancelButtonTitle:@"OK"
												  otherButtonTitles:nil];
			alert.tag = MFMailComposeResultSaved;
			[alert show];
			[alert release];			
		}
			break;
		case MFMailComposeResultSent:
		{
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mail"
															message:@"Send Mail Success"
														   delegate:self
												  cancelButtonTitle:@"OK"
												  otherButtonTitles:nil];
			alert.tag = MFMailComposeResultSent;
			[alert show];
			[alert release];			
		}
			break;
		case MFMailComposeResultFailed:
		{
			if([error.domain isEqualToString:MFMailComposeErrorDomain]) {
				UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mail"
																message:(error.code==MFMailComposeErrorCodeSendFailed)?@"Send Mail Failed":
									  (error.code==MFMailComposeErrorCodeSaveFailed)?@"Save Draft Failed":
									  @"Unknown"
															   delegate:nil
													  cancelButtonTitle:@"OK"
													  otherButtonTitles:nil];
				[alert show];
				[alert release];
			} else {
				UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Mail"
																message:@"Unknown Error"
															   delegate:nil
													  cancelButtonTitle:@"OK"
													  otherButtonTitles:nil];
				[alert show];
				[alert release];
			}
		}
			break;
		default:
			break;
	}
}


@end

